package applications.studentApplicationForm.ui.lib;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.objectweb.fractal.fraclet.annotations.Attribute;
import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;
import org.objectweb.fractal.fraclet.types.Cardinality;

@Component(provides = {@Interface(name = "jPanelItf", signature = applications.studentApplicationForm.ui.lib.PanelInterface.class), @Interface(name = "jPanelColAttributes", signature = applications.studentApplicationForm.ui.lib.PanelColInterface.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})
public class FractalColWithButton extends JPanel implements PanelInterface, ontocompo.api.OntoCompoIntrospection, PanelColInterface //, ActionListener

{
	
	protected org.objectweb.fractal.api.Component c;


	@Attribute
	protected String semantic_uid;
	
	
	@Attribute
	protected int nbLignes;
	
	@Attribute
	protected int widthCell;
	
	@Attribute
	protected int heightCell;
	
	// JButton [] myJButtonElements ;
	@Requires(name="yearchoice", cardinality=Cardinality.COLLECTION)
	Map<String, JButtonInterface> yearchoice = new HashMap<String, JButtonInterface>();
	
// 	useless avec les JButton en tant que composants...
//	@Requires(name = "studentformdirectoryservice")
//	private applications.studentApplicationForm.fc.api.StudentFormDirectoryService studentformdirectoryservice;

	
	protected int count = 0;
	
	
	Dimension d = new Dimension(0,0);
	Dimension d_elements = new Dimension(0,0);
	
		
	@Override
	public void init() {
		setLayout(null);
		d.width = widthCell+getMarge()*2;
		d.height = heightCell*nbLignes+getMarge()*2;
		
		setSize(d);
		setPreferredSize(d);
		setMaximumSize(d);
		setMinimumSize(d);
		
		d_elements.width = widthCell;
		d_elements.height = heightCell;
		
		// myJButtonElements = new JButton[nbLignes];
		
		
//		for(int i = nbLignes-1; i >= 0 ; i--)
//		{
//			myJButtonElements[i] = new JButton(semantic_uid);
//			myJButtonElements[i].addActionListener(this);
//			modifyComponent(i);
//			
//			super.add(myJButtonElements[i], 0);
//
//		
//			System.out.println(" construction de "+semantic_uid+" : "+i);
//
//		}
		
		assert(yearchoice.size() == nbLignes);
				
		for(int i = nbLignes; i >= 1 ; i--)
		{
			System.out.println("yearchoice"+i+" / "+yearchoice.get("yearchoice"+i));
			JButtonInterface elemComponent =yearchoice.get("yearchoice"+i);
			elemComponent.init();
			JButton elem = (JButton) elemComponent.getComponentItSelf();
			modifyComponent(i-1);
			super.add(elem, 0);
		}
		
	}
	
	
	public java.awt.Component add(java.awt.Component c) {
		if ((c instanceof JButton) && (count < nbLignes))
		{
			java.awt.Component result = add(c, count);
//			((JButton) result).addActionListener(this);
			if (result != null) count++;
			
			return result;
		}
		else return null;
	}
	
	@Override
	public java.awt.Component add(java.awt.Component c, int i) {
		if ((c instanceof JButton) && (i >= 0) && (i < nbLignes))
		{
//			remove(myJButtonElements[i]);
//			myJButtonElements[i].removeActionListener(this);
//			myJButtonElements[i] = (JButton) c;
//			myJButtonElements[i].addActionListener(this);
//
//			super.add(myJButtonElements[i], i);
			
			
			JButton elem = (JButton) yearchoice.get("yearchoice"+(i+1)).getComponentItSelf();
			remove(elem);
			yearchoice.remove(i);
			elem.add(c, i);

			super.add(c, i);
			
			modifyComponent(i);
			
			validate();
			return c;
		}
		else return null;
	}
	
	
	private void modifyComponent(int i)
	{
		JButton elem = (JButton) yearchoice.get("yearchoice"+(i+1)).getComponentItSelf();
		
		elem.setSize(d_elements);
		elem.setPreferredSize(d_elements);
		elem.setMaximumSize(d_elements);
		elem.setMinimumSize(d_elements);
		// elem.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		
		elem.setLocation(getMarge(), heightCell*i+getMarge());
		/*
		 * elem.setBackground(Color.white);
		 * 		elem.setOpaque(true);

		 */
	}
	
	public void setText(String msg, int row)
	{
		System.out.println("coucou (JButton) , pb dans un setText "+msg+" pour la ligne "+(row+1)+" / "+ nbLignes+" / "+semantic_uid);
		assert(yearchoice.size() == nbLignes);
		if ( (row >= 0) && (row < nbLignes))
		{
			if (yearchoice.size() == 0) init();
			
			JButton elem = (JButton) yearchoice.get("yearchoice"+(row+1)).getComponentItSelf();
			
			if (elem != null) elem.setText(msg);
			else System.out.println("Erreur... (JButton) dans un setText "+semantic_uid);
		}

	}


	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}


	@Override
	public void update(Object o) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return c;
	}


	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}


	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return semantic_uid;
	}


	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		semantic_uid =uid;
	}


	@Override
	public int getMarge() {
		// TODO Auto-generated method stub
		return 3;
	}


//	@Override
//	public void actionPerformed(ActionEvent arg0) {
//		if (arg0.getSource() instanceof JButton)
//			{
//			JButton b = (JButton) arg0.getSource();
//			String text = b.getText();
//			
//			// test pour verif ?
//			studentformdirectoryservice.setYearValue(text);
//			}
//		
//		
//	}
}
